<?php
	class irc {
		private $server;
		private $port;
		private $ssl;
		private $con; //the connection holder
		private $cli;

		public function __construct ($rServer,$rPort,$rSSL=false)
		{
			$this->server = $rServer;
			$this->port = $rPort;
			$this->ssl = $rSSL;
			$this->cli = new cli;

			if(is_numeric($rPort))
			{
				//try/catch				
				$this->createConnection();
			}
			else
			{	
			        throw new Exception('Port not numeric');
				die();
			}
		}

		public function __destruct()
		{
			 $this->killCon();
		         fclose($this->con);
		}
	

	        private function createConnection()
	        {
	//     	        $this->con = fsockopen($this->server, $this->port, $erno, $errstr, 30);
	//		$this->con = socket_create('AF_INET'
//			$mode = ($this->ssl) ? 'tls://' : 'tcp://';
			$mode = ($this->ssl) ? 'tls://' : 'tcp://';
			$this->con = stream_socket_client($mode.$this->server.':'. $this->port, $errno, $errstr, 30);
//			$this->con = stream_socket_client('tcp://'.$this->server.':'. $this->port, $errno, $errstr, 30);

	        }

		private function checkCon()
		{
			return fgets($this->con, 512);
		}

		public function getData()
		{
			return trim($this->checkCon());
		}

		protected function sendTrueRaw($raw)
		{
			fwrite($this->con,$raw);
		}

		public function sendRaw($raw)
		{
			//send data with an end
			$this->sendTrueRaw($raw."\r\n");
			$this->cli->setColor(48);
			$this->cli->write($raw);
			$this->cli->setColor(0);
		}

		public function killCon($msg='')
		{
			$this->sendRaw("QUIT :$msg");
		}

		public function getStream()
		{
			//use with care - mostly for the stream_select that is above the irc connections thing.
			//I'd like to turn even THAT into a class so I don't have to worry about this messing things up!
			return $this->con;
		}
	}
?>
